/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.equipment.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.equipment.entity.EleEquipment;
import com.thinkgem.jeesite.modules.equipment.entity.EleEquipmentNocheck;
import com.thinkgem.jeesite.modules.equipment.service.EleEquipmentNocheckService;
import com.thinkgem.jeesite.modules.equipment.service.EleEquipmentService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 未审核接入设备管理Controller
 * @author ZC
 * @version 2017-06-20
 */
@Controller
@RequestMapping(value = "${adminPath}/equipment/eleEquipmentNocheck")
public class EleEquipmentNocheckController extends BaseController {

    @Autowired
    private EleEquipmentService eleEquipmentService;

	@Autowired
	private EleEquipmentNocheckService eleEquipmentNocheckService;
	
	@ModelAttribute
	public EleEquipmentNocheck get(@RequestParam(required=false) String id) {
		EleEquipmentNocheck entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleEquipmentNocheckService.get(id);
		}
		if (entity == null){
			entity = new EleEquipmentNocheck();
		}
		return entity;
	}
	
	@RequiresPermissions("equipment:eleEquipmentNocheck:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleEquipmentNocheck eleEquipmentNocheck, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EleEquipmentNocheck> page = eleEquipmentNocheckService.findPage(new Page<EleEquipmentNocheck>(request, response), eleEquipmentNocheck); 
		model.addAttribute("page", page);
		return "modules/equipment/eleEquipmentNocheckList";
	}

	@RequiresPermissions("equipment:eleEquipmentNocheck:view")
	@RequestMapping(value = "form")
	public String form(EleEquipmentNocheck eleEquipmentNocheck, Model model) {
        User thisuser = UserUtils.getUser();
        eleEquipmentNocheck.setCompany(thisuser.getCompany());
        // 审核控制没有关闭 并且没有用户权限的时候
        if (!UserUtils.getSubject().isPermitted("equipment:eleEquipment:check")){
            eleEquipmentNocheck.setOffice(thisuser.getOffice());
        }
		model.addAttribute("eleEquipmentNocheck", eleEquipmentNocheck);
		return "modules/equipment/eleEquipmentNocheckForm";
	}

	@RequiresPermissions("equipment:eleEquipmentNocheck:view")
	@RequestMapping(value = "save")
	public String save(EleEquipmentNocheck eleEquipmentNocheck, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eleEquipmentNocheck)){
			return form(eleEquipmentNocheck, model);
		}
        eleEquipmentNocheckService.delete(eleEquipmentNocheck);

        EleEquipment eleEquipment = (EleEquipment)JsonMapper.fromJsonString(JsonMapper.toJsonString(eleEquipmentNocheck), EleEquipment.class);
        eleEquipment.setCompany(eleEquipmentNocheck.getCompany());
        eleEquipment.setOffice(eleEquipmentNocheck.getOffice());
        eleEquipment.setType(eleEquipmentNocheck.getType());
        eleEquipment.setId(null);
        eleEquipmentService.save(eleEquipment);

		addMessage(redirectAttributes, "保存未审核接入设备成功");
		return "redirect:"+Global.getAdminPath()+"/equipment/eleEquipmentNocheck/?repage";
	}
	
	@RequiresPermissions("equipment:eleEquipmentNocheck:view")
	@RequestMapping(value = "delete")
	public String delete(EleEquipmentNocheck eleEquipmentNocheck, RedirectAttributes redirectAttributes) {
		eleEquipmentNocheckService.delete(eleEquipmentNocheck);
		addMessage(redirectAttributes, "删除未审核接入设备成功");
		return "redirect:"+Global.getAdminPath()+"/equipment/eleEquipmentNocheck/?repage";
	}

}